<template>
  <div class="box">
    <div class="formBox">
      <h1>vue-project</h1>
      <img src="@/assets/images/logo.png" alt="" />
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toLogin">登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/home";
export default {
  data() {
    return {
      form: {
        userName: "admin",
        password: 123456,
      },
    };
  },
  methods: {
    async toLogin() {
      if (!this.form.userName || !this.form.password) {
        return this.$message.error("账号或密码不能为空");
      }
      const { data: res } = await login(this.form);
      if (res.code !== 200) return this.$message.error(res.info);
      this.$store.commit("setToken", res.data.token);
      this.$router.replace("/home");
      this.$message.success(res.info);
    },
    reset() {
      this.form.userName = "";
      this.form.password = "";
    },
  },
  mounted(){
    // console.log(process.env)
  }
};
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100vh;
  background: url("~@/assets/images/loginBg.jpg") no-repeat;
  background-size: 100% 100%;
}
.formBox {
  padding: 70px 50px 30px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
  opacity: 0.9;
  & > h1 {
    position: absolute;
    top: 16px;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 50%;
  }
  & > img {
    width: 120px;
    height: 120px;
  }
  .el-form {
    width: 400px;
    transform: translateY(20px);
  }
}
</style>